package org.example.leetcode;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * file:ZiFuChuan.class
 * function:字符串相关Algorithm
 *
 * @author shaodan wang
 * @date 2024-05-29
 */


public class ZiFuChuan {


    /**
     * 反转字符串2
     * https://leetcode.cn/problems/reverse-string-ii/
     * 找2k个 反正前k个，
     * 找到最后的不足2K 但是大于k正常反转前k个，小于k个则反转剩余的
     * 这里重点是怎么更好地处理尾部剩余的情况
     *
     * @param s
     * @param k
     * @return
     */
    public String reverseStr(String s, int k) {
        char[] charStr = s.toCharArray();
        for (int i = 0; i <= charStr.length; i = i + k * 2) {
            //eg: k = 3, string 为 abcdefghij
            //其中，i 是需要反转的起始位置，如k为3 ，
            // 第一次 i=0  end = i + k = 3,反转 0 - 2
            //第二次 i = k * 2 = 6 end = i + k = 9  反转 6-8的位置，到这里就结束了
            //也就是说 判断的时候，是2*k的步数，实际转换的时候是k的步数
            int end = Math.min(charStr.length, i + k);
            reverse(charStr, i, end - 1);
        }
        return new String(charStr);
    }

    private char[] reverse(char[] charStr, int from, int to) {
        if (charStr.length < to || from >= to) {
            return charStr;
        }
        int start = from;
        int end = to;

        while (start < end) {
            char temp = charStr[start];
            charStr[start] = charStr[end];
            charStr[end] = temp;
            start++;
            end--;
        }
        return charStr;
    }

    @Test
    public void testReverseStr() {
        System.out.println(reverseStr("abcdefghij", 3));
    }

    /**
     * 替换字符串
     *
     * @param s
     * @return
     */
    public String replaceNumber(String s) {
        if (s == null || s.equals("")) {
            return s;
        }
        int len = 0;//需要拓展的长度
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                len += 5;//注意这里为什么仅仅加上5,因为这个数字本身就是是一位，所以只需要5就是六位
            }
        }
        char[] strChar = s.toCharArray();
        char[] resChar = new char[s.length() + len];
        int j = resChar.length - 1;
        for (int i = strChar.length - 1; i >= 0; i--) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                resChar[j--] = 'r';
                resChar[j--] = 'e';
                resChar[j--] = 'b';
                resChar[j--] = 'm';
                resChar[j--] = 'u';
                resChar[j--] = 'n';
            } else {
                resChar[j--] = s.charAt(i);
            }
        }
        return new String(resChar);
    }

    @Test
    public void testReplaceNumber() {
        System.out.println(replaceNumber("a5bcdefg7h88"));

    }

    public int[] distributeCandies(int candies, int num_people) {
        int[] res = new int[num_people];
        int total = 0; //
        final int c = candies;
        for (int i = 1; ; i++) {
            if (candies <= 0) {
                res[(i - 1) % num_people] = res[(i - 1) % num_people] + c - total;
                break;
            } else {
                res[(i - 1) % num_people] = res[(i - 1) % num_people] + i;
            }
            total = total + i;
            candies = candies - i - 1;
        }
        return res;
    }

    public String convert(String s, int numRows) {
        return "";

    }


    @Test
    public void testDistributeCandies() {
        System.out.println(Arrays.toString(distributeCandies(7, 4)));

    }


}
